//
//  UIView+frame.swift
//  Pods
//
//  Created by zhu xietong on 2017/8/14.
//
//

import Foundation
import Eelay

open class EeView: JoView {
    var __loadIndex = 0{
        didSet{
            if __loadIndex == 1{
                viewDidLoad()
            }
        }
    }
    open func viewDidLoad(){
        
    }
//    open override func willMove(toSuperview newSuperview: UIView?) {
//        super.willMove(toSuperview: newSuperview)
//        __loadIndex = __loadIndex + 1
//    }
    
    
    open override func didMoveToSuperview() {
        super.didMoveToSuperview()
        __loadIndex = __loadIndex + 1
    }
    
}

open class ParallaxHeader:EeView,EeActionDelegate{
        
    public var justHeight:CGFloat{
        get{
            let just = mo.controller?.topLayoutGuide.length ?? TP.topHeight
            return just
        }
    }
    public var miniHeigth:CGFloat{
        get{
            return self.frame.size.height + justHeight
        }
    }
    
    public var heightLayoutConstraint = NSLayoutConstraint()
    
    public let contentView = UIView()
    
    
    public func active(onwer: Any, actionID: String, data: NSMutableDictionary) {
        if actionID.hasPrefix("tap")
        {
        }
    }
    
    override open func mo_model_reload() {
        
    }
    

    
    
    override open func addLayoutRules() {
        self.backgroundColor = UIColor.white
    }
    
    open override func viewDidLoad() {
        
        self.eelay = [
            [contentView,[ee.L.R.B]],
        ]
        self.heightLayoutConstraint = NSLayoutConstraint(item: contentView, attribute:.height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1, constant: self.miniHeigth)
        self.addConstraint(heightLayoutConstraint)
    }
    
  
    
    override open func mo_set_controller(controller: UIViewController) {
        
       
    }
    
    open func scrollViewDidScroll(scrollView: UIScrollView) {
        let offsetY = (-scrollView.contentOffset.y) - justHeight

        if offsetY < 0{
            return
        }
        self.heightLayoutConstraint.constant = miniHeigth+offsetY
        self.contentView.layoutIfNeeded()
        contentView.clipsToBounds = offsetY <= 0
    }
}
extension UIView
{
    
    
    public func setContentConstrainToHighLever()
    {
        self.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 1000), for: NSLayoutConstraint.Axis.horizontal)
        self.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 1000), for: NSLayoutConstraint.Axis.vertical)
        
        self.setContentHuggingPriority(UILayoutPriority(rawValue: 1000), for: NSLayoutConstraint.Axis.horizontal)
        self.setContentHuggingPriority(UILayoutPriority(rawValue: 1000), for: NSLayoutConstraint.Axis.vertical)
    }
    
    public func setContentConstrainToLowLever()
    {
        self.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 100), for: NSLayoutConstraint.Axis.horizontal)
        self.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 100), for: NSLayoutConstraint.Axis.vertical)
        
        self.setContentHuggingPriority(UILayoutPriority(rawValue: 100), for: NSLayoutConstraint.Axis.horizontal)
        self.setContentHuggingPriority(UILayoutPriority(rawValue: 100), for: NSLayoutConstraint.Axis.vertical)
        
    }
}
